<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <!-- import CSS -->
    <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
</head>
<body>
<div id="app">
    <el-form ref="form" :model="book" label-width="80px">
        <el-form-item label="书号">
            <el-input v-model="book.number" :disabled="readOnly"></el-input>
        </el-form-item>
        <el-form-item label="书名">
            <el-input v-model="book.name" :disabled="readOnly"></el-input>
        </el-form-item>
        <el-form-item label="作者">
            <el-input v-model="book.author" :disabled="readOnly"></el-input>
        </el-form-item>
        <el-form-item>
            <el-button type="primary" @click="onSubmit" v-if="!readOnly">保存</el-button>
            <el-button @click="onCancel">取消</el-button>
        </el-form-item>
    </el-form>
</div>
</body>
<!-- import Vue before Element -->
<script src="https://unpkg.com/vue@2/dist/vue.js"></script>
<!-- import JavaScript -->
<script src="https://unpkg.com/element-ui/lib/index.js"></script>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script src="./js/utils.js"></script>
<script>
    new Vue({
        el: '#app',
        data: function () {
            return {
                book: {
                    id: null,
                    name: '',
                    number: '',
                    author: ''
                },
                readOnly: false
            }
        },
        methods: {
            onCancel() {
                history.go(-1);
            },
            onSubmit() {
                console.log('submit!');
                // 保存或者更新书本信息
                this.saveOrUpdate();
            },
            findBookById(id) {
                let url = '/book/findBookById/' + id;
                // 特别注意这个
                var self = this;
                axios.get(url)
                    .then(function (res) {
                        console.log(res.data);
                        self.book = res.data;
                    })
                    .catch(function (error) {
                        console.log(error);
                    });
            },
            saveOrUpdate() {
                let url = '/book/saveOrUpdate';
                // 特别注意这个
                var self = this;
                console.log(self.book);
                axios.post(url, self.book)
                    .then(function (res) {
                        console.log(res.data);
                        self.book = res.data;
                        self.$message({
                            message: '数据更新成功',
                            type: 'success'
                        });
                    })
                    .catch(function (error) {
                        self.$message.error('数据更新失败');
                    });
            }
        },
        created() {
            // 获取连接上的ID
            let id = getUrlKey("id");
            let readOnly = getUrlKey("readOnly");
            this.readOnly = JSON.parse(readOnly);
            console.log(id);
            if (id) {
                // 通过 id 查询明细，赋值给下面的 book 对象
                this.findBookById(id);
            }
        }
    })
</script>
</html>